<?php
namespace app\common\business;

use app\common\model\mysql\Category as categoryModel;
class Category{
	protected $categoryObj = null;
	public function __construct(){
		$this->categoryObj = new categoryModel();
	}
 
	// 添加数据
	public function add($data){
		$data['status'] = config('status.mysql.normal');
		$this->categoryObj->save($data);
		//return $this->categoryObj->getLastInsID();
		return $this->categoryObj->id;
	}

	// 获取正常分类
	public function getNormalCategorys(){
		$field = "id,name,pid";
		$categorys = $this->categoryObj->getNormalCategorys($field);
		
		if(!$categorys){
			return $categorys;
		}
		return $categorys->toArray();
	}
	public function getNormalCategorys2($where=[],$field = "id,name,pid"){
		
		$categorys = $this->categoryObj->getNormalCategorys2($where,$field);
		
		if(!$categorys){
			return $categorys;
		}
		return $categorys->toArray();
	}

	// 获取正常分类
	public function getAllNormalCategorys(){
		$field = "id as category_id,name,pid";
		$categorys = $this->categoryObj->getNormalCategorys($field);
		
		if(!$categorys){
			return $categorys;
		}
		return $categorys->toArray();
	}

	//获取正常一级分类
	public function getNormalByPid($pid = 0,$field = 'id,name,pid'){
		try{
			$res = $this->categoryObj->getNormalByPid($pid,$field);
		}catch(\Exception $e){
			// 记录日志
			return [];
		}

		return $res->toArray();
	}

	// 获取分类列表
	public function getLists($data,$num){
		$list = $this->categoryObj->getLists($data,$num);
		//halt($list->render());
		if(!$list){
			return [];
		}
		
		$result = $list->toArray();
		$result['render'] = $list->render();
		
		$pids = array_column($result['data'], "id");
		if(count($pids)){
			$idCountResult = $this->categoryObj->getChildCountInPids(['pid'=>$pids]);
			$idCountResult = $idCountResult->toArray();

			$idCounts = [];
			foreach($idCountResult as $countResult){
				$idCounts[$countResult['pid']] = $countResult['count'];
			}
		}
		if($result['data']){
			foreach($result['data'] as $k=>$v){
				$result['data'][$k]['childCount'] = $idCounts[$v['id']] ?? 0; 
			}
		}
		return $result;
	}

	// 根据ID获取记录
	public function getById($id){
		$result = $this->categoryObj->find($id);
		if(empty($result)){
			return [];
		}
		return $result->toArray();
	}
	// 排序
	public function listorder($id,$listorder){
		//查询id对应的记录是否存在
		$res = $this->getById($id);
		if(!$res){
			throw new \think\Exception('不存在该条记录');
		}
		$data = [
			"listorder" => $listorder,
		];
		try{
			$res = $this->categoryObj->updateById($id,$data);
		}catch(\Exception $e){
			//记录日志
			return false;
		}
		return $res;
	}

	// 更新状态
	public function status($id,$status){
		//查询id对应的记录是否存在
		$res = $this->getById($id);
		if(!$res){
			throw new \think\Exception("不存在该条记录");
		}
		if($res['status'] == $status){
			throw new \think\Exception("状态没变");
		}

		$data = [
			'status' => $status,
		];

		try{
			$res = $this->categoryObj->updateById($id,$data);
		}catch(\Exception $e){
			//记录日志
			return false;
		}

		return $res;
	}
}